package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.config.PrivateConfig;
import com.cjack.frenchexcel.exception.BankCardExsitException;
import com.cjack.frenchexcel.exception.BankCardNotExsitException;
import com.cjack.frenchexcel.model.AdminUserModel;
import com.cjack.frenchexcel.model.MooncardCardModel;
import com.cjack.frenchexcel.service.MoonCardService;
import com.cjack.frenchexcel.service.MooncardCardService;
import com.cjack.frenchexcel.service.RoleService;
import com.cjack.frenchexcel.service.UserAdminService;
import com.cjack.frenchexcel.table.AdminUserTable;
import com.cjack.frenchexcel.util.CustomerStringUtil;
import com.cjack.frenchexcel.util.DateFormatUtil;
import com.cjack.frenchexcel.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/userAdmin")
public class UserAdminEndpoint extends BaseEndpoint {

    @Autowired
    UserAdminService service;
    @Autowired
    RoleService roleService;
    @Autowired
    MoonCardService moonCardService;
    @Autowired
    MooncardCardService mooncardCardService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list() {
        List<AdminUserModel> datas = service.findByBesideCode();
        for (AdminUserModel data : datas) {
            data.setCreateTimeStr(DateFormatUtil.format(data.getCreateTime(), DateFormatUtil.DATE_RULE_4));
            //查询该用户的额度信息
            MooncardCardModel mooncard = mooncardCardService.getFirstCard(data.getId());
            data.setMooncard(mooncard);
        }
        return AjaxResult.SUCCESS(datas);
    }

    /**
     * 说动触发额度相关的计算
     *
     * @return
     */
    @RequestMapping(value = "/handleLimitCount", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult handleLimitCount() {
        moonCardService.handleDayMooncardLimit();
        return AjaxResult.SUCCESS();
    }

    /**
     * 列表数据，供其它下拉的地方调用
     *
     * @return 需要直接返回List<Model>这种格式的数据
     */
    @RequestMapping(value = "/listForSelect", method = RequestMethod.GET)
    @ResponseBody
    public List<AdminUserModel> listForSelect() {
        return service.findByBesideCode();
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate(AdminUserModel model) {
        if (service.checkRepeat(model)) {
            return AjaxResult.ERROR(ErrorCodeDefine.USERNAME_REPEAT, ErrorCodeDefine.USERNAME_REPEAT_MSG);
        }
        String newPassword = "";
        if (isAdd(model.getId())) {
            model.setCreateTime(new Date());
            //新增的时候自动设置员工工号  现在员工工号由管理员手动输入
//                model.setJobNumber( CustomerStringUtil.randomJobNum());
            newPassword = CustomerStringUtil.randomStr(6);
            model.setPassword(Md5Util.stringToMD5(newPassword));
            model.setPasswordStatus("SYSTEM");
            model.setReviewStatus("PASS");
            //
            if (model.getRoleId() == null || model.getRoleId().equals(0l)) {
                //新增员工，如果没有选择角色，默认制定为 员工角色
                model.setRoleId(roleService.getByCode(PrivateConfig.EMPLOEE_CODE).getId());
            }
        }
        //先把role实体给转换了
        if (model.getRoleId() != null) {
            model.setRole(roleService.get(model.getRoleId()));
        }
        service.add(model);
        AdminUserTable findUser = service.getByJobNumber(model.getJobNumber());
        if (Objects.nonNull(findUser)) {
            model.setCompanyId(findUser.getId());
            model.setId(findUser.getId());
            service.add(model);
        }
        if (isAdd(model.getId())) {
            String res = "添加成功,新员工的登录密码：" + newPassword + "，请保存好该员工的新密码;";
            return AjaxResult.SUCCESS(res);
        } else {
            return AjaxResult.SUCCESS();
        }
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult del(@RequestParam("ids") String ids) {
        service.del(ids);
        return AjaxResult.SUCCESS("删除成功");
    }

    /**
     * 重置用户密码
     *
     * @return
     */
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult resetPassword(Long id) {
        String newPassword = service.resetPassword(id);
        return AjaxResult.SUCCESS("重置密码成功，请保存好该用户的新密码：" + newPassword);
    }

    /**
     * 审核
     *
     * @return
     */
    @RequestMapping(value = "/review", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult review(AdminUserModel model) {
        service.review(model);
        return AjaxResult.SUCCESS();
    }
}
